#include "watchdiinstructionview.h"
#include "ui_watchdiinstructionview.h"

WatchDIInstructionView::WatchDIInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WatchDIInstructionView)
{
    ui->setupUi(this);
    setGeometry(5,50,451,544);
    initialDisplayStyle(styleId);
    ui->radioButton_watchTime->setChecked(true);
    ui->radioButton_counter->setChecked(false);
}

void WatchDIInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

WatchDIInstructionView::~WatchDIInstructionView()
{
    delete ui;
}

void WatchDIInstructionView::getInput(WatchDIInstruction &instructionParaOut)
{
    instructionParaOut.dIIndex=ui->lineEdit_DIIndex->text().toInt();
    instructionParaOut.dISwitch=ui->comboBox_DIswitch->currentText();
    if(ui->radioButton_watchTime->isChecked())
    {
        instructionParaOut.watchTime=ui->lineEdit_watchTime->text().toDouble();
        instructionParaOut.watchDITyde=E_WATCHDI_TYPE_TIME;
    }
    else
    {
        instructionParaOut.counterIndex=ui->lineEdit_counter->text().toInt();
        instructionParaOut.watchDITyde=E_WATCHDI_TYPE_COUNTER;
    }
    instructionParaOut.dOIndex=ui->lineEdit_DOIndex->text().toInt();
    instructionParaOut.dOSwitch=ui->comboBox_DOswitch->currentText();
    instructionParaOut.comment=ui->note_lineEdit->text();


}
void WatchDIInstructionView::updateView()
{
    ui->lineEdit_DIIndex->setText(QString::number(instructionPara.dIIndex));
    ui->comboBox_DIswitch->setCurrentText(instructionPara.dISwitch);
    switch (instructionPara.watchDITyde) {
    case E_WATCHDI_TYPE_TIME:
    ui->lineEdit_watchTime->setText(QString::number(instructionPara.watchTime,10,2));
    ui->radioButton_watchTime->setChecked(true);
    ui->radioButton_counter->setChecked(false);
    break;
    case E_WATCHDI_TYPE_COUNTER:
    ui->radioButton_watchTime->setChecked(false);
    ui->radioButton_counter->setChecked(true);
    ui->lineEdit_counter->setText(QString::number(instructionPara.counterIndex));
    break;

    default:
        break;
    }
    ui->lineEdit_DOIndex->setText(QString::number(instructionPara.dOIndex));
    ui->comboBox_DOswitch->setCurrentText(instructionPara.dOSwitch);
    ui->note_lineEdit->setText(instructionPara.comment);

}

void WatchDIInstructionView::changeLanguage(int language)
{

}

void WatchDIInstructionView::finishEditInstruction(WatchDIInstruction instructionParaIn, QString &instructionStrOut)
{
       instructionStrOut.clear();
       instructionStrOut="Watch DI[";
       instructionStrOut+=QString::number(instructionParaIn.dIIndex)+"]=";
       instructionStrOut+=instructionParaIn.dISwitch+" ";
       switch (instructionParaIn.watchDITyde) {
       case E_WATCHDI_TYPE_TIME:
        //Watch DI[1]=ON 0.5 sec DO[5]=OFF;//ssss
           instructionStrOut+=QString::number(instructionParaIn.watchTime,10,2)+" sec ";

           break;
       case E_WATCHDI_TYPE_COUNTER:

           instructionStrOut+="R["+QString::number(instructionParaIn.counterIndex)+"] sec ";
           break;
       default:
           break;
       }
       instructionStrOut+="DO["+QString::number(instructionParaIn.dOIndex)+"]="
                         +instructionParaIn.dOSwitch
                         +";//"
                         +instructionParaIn.comment;
}

void WatchDIInstructionView::on_radioButton_watchTime_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_watchTime->setChecked(true);
        ui->radioButton_counter->setChecked(false);
    }
    else
    {

    }
}

void WatchDIInstructionView::on_radioButton_counter_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_watchTime->setChecked(false);
        ui->radioButton_counter->setChecked(true);
    }
    else
    {

    }
}

void WatchDIInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->setText("");

}

void WatchDIInstructionView::on_save_Btn_clicked()
{
    QString str1 = ui->lineEdit_DIIndex->text();
    QString str2 = ui->lineEdit_DOIndex->text();
    QString str3 = ui->lineEdit_counter->text();
    QString str4 = ui->lineEdit_watchTime->text();

    QString pattern("^[0-9]+(.[0-9]{1,5})?$");
    QString pattern1("^[0-9]+$");
    QRegExp rxRoller(pattern);
    QRegExp rxRoller1(pattern1);
    if(!rxRoller1.exactMatch(str1) || !rxRoller1.exactMatch(str2))
    {
       emit showErrorMsgBox(QString("存在非法字符，请修改！"));
       return;
    }
    else if(ui->radioButton_watchTime->isChecked() && !rxRoller.exactMatch(str4))
    {
        emit showErrorMsgBox(QString("存在非法字符，请修改！"));
        return;
    }
    else if(ui->radioButton_counter->isChecked() && !rxRoller1.exactMatch(str3))
    {
        emit showErrorMsgBox(QString("存在非法字符，请修改！"));
        return;
    }
    else
    {
    getInput(instructionPara);
    finishEditInstruction(instructionPara,instructionStr);
//    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionStr);
    }
}

